﻿Public Class PhotoPost

    Private acct As TumblePower.tumblr.Account
    Private TumblrData As TumblePower.tumblr
    Dim filelocation As String

    Public Sub New(ByVal account As TumblePower.tumblr.Account)
        InitializeComponent()
        acct = account
    End Sub

    Private Sub CreateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateButton.Click
        CreateButton.Text = "Please Wait..."
        CreateButton.Enabled = False
        CancelButton.Enabled = False

        TumblrData = New TumblePower.tumblr
        Dim t As New TumblePower.tumblr.Photo
        With t
            .Email = acct.Email
            .Password = acct.Password
            .Data = filelocation
            .Caption = CaptionTextBox.Text
            .Tags = TagsTextBox.Text
            .IsPrivate = PrivatePostCheckBox.Checked
        End With
        Dim ap As New TumblePower.tumblr.apostPhoto(AddressOf TumblrData.postPhoto)

        Dim result As IAsyncResult = ap.BeginInvoke(t, New AsyncCallback(AddressOf CallbackMethod), ap)
    End Sub

    Private Sub CallbackMethod(ByVal ar As IAsyncResult)
        If Me.InvokeRequired Then
            Me.BeginInvoke(New AsyncCallback(AddressOf CallbackMethod), ar)
            Exit Sub
        End If

        Dim caller As TumblePower.tumblr.apostPhoto = DirectCast(ar.AsyncState, TumblePower.tumblr.apostPhoto)
        Dim returnValue As TumblePower.Status = caller.EndInvoke(ar)

        If returnValue.Code <> 201 Then
            MsgBox(returnValue.Msg)
        Else
            MsgBox("Post Created")
            Me.Close()
            CaptionTextBox.Clear()
            TagsTextBox.Clear()
        End If
        CreateButton.Text = "Create Post"
        CancelButton.Enabled = True
        CreateButton.Enabled = True
    End Sub

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelButton.Click
        If MsgBox("Cancel editing this post? All unpublished changes will be lost.", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Me.Close()
            CaptionTextBox.Clear()
            TagsTextBox.Clear()
        End If
    End Sub

    Private Sub UploadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadButton.Click
        Dim open As New OpenFileDialog()
        open.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.GIF)|*.BMP;*.JPG;*.JPEG;*.GIF|All files (*.*)|*.*"
        open.CheckFileExists = True
        open.Title = "Open Photo"
        open.ShowDialog(Me)
        Try
            filelocation = open.FileName()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub BoldToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BoldToolStripMenuItem.Click
        Dim InsertStringBefore As String = "<b>"
        Dim InsertStringAfter As String = "</b>"
        Dim SelStart As Integer = CaptionTextBox.SelectionStart
        Dim SelLength As Integer = CaptionTextBox.SelectionLength

        CaptionTextBox.Text = CaptionTextBox.Text.Insert(SelStart + SelLength, InsertStringAfter)
        CaptionTextBox.Text = CaptionTextBox.Text.Insert(SelStart, InsertStringBefore)
    End Sub

    Private Sub ItalicToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ItalicToolStripMenuItem.Click
        Dim InsertStringBefore As String = "<i>"
        Dim InsertStringAfter As String = "</i>"
        Dim SelStart As Integer = CaptionTextBox.SelectionStart
        Dim SelLength As Integer = CaptionTextBox.SelectionLength

        CaptionTextBox.Text = CaptionTextBox.Text.Insert(SelStart + SelLength, InsertStringAfter)
        CaptionTextBox.Text = CaptionTextBox.Text.Insert(SelStart, InsertStringBefore)
    End Sub

    Private Sub StrikeoutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StrikeoutToolStripMenuItem.Click
        Dim InsertStringBefore As String = "<strike>"
        Dim InsertStringAfter As String = "</strike>"
        Dim SelStart As Integer = CaptionTextBox.SelectionStart
        Dim SelLength As Integer = CaptionTextBox.SelectionLength

        CaptionTextBox.Text = CaptionTextBox.Text.Insert(SelStart + SelLength, InsertStringAfter)
        CaptionTextBox.Text = CaptionTextBox.Text.Insert(SelStart, InsertStringBefore)
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
        CaptionTextBox.Cut()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        CaptionTextBox.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        CaptionTextBox.Paste()
    End Sub
End Class